package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.BgDeviceHistoryLog;
import com.example.entity.BpDeviceHistoryLog;
import com.example.service.IBgDeviceHistoryLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ysr_xian
 * @since 2022-05-29
 */
@RestController
@RequestMapping("/bgDeviceHistoryLog")
public class BgDeviceHistoryLogController {
    @Autowired
    private IBgDeviceHistoryLogService bgDeviceHistoryLogService;

    @GetMapping("/list")
    public List<BgDeviceHistoryLog> list(@RequestParam(defaultValue = "") String deviceName,
                                         @RequestParam(defaultValue = "") String startDate,
                                         @RequestParam(defaultValue = "") String endDate) {
        QueryWrapper<BgDeviceHistoryLog> queryWrapper = new QueryWrapper<>();
        // 日期格式化
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (!startDate.equals("") && !endDate.equals("")) {
            queryWrapper.between("gmt_create",
                    LocalDate.parse(startDate, df).atStartOfDay(),       // 获取当前时间的0点
                    LocalDate.parse(endDate, df).atTime(LocalTime.MAX)); // 获取当前时间的24点
        }
        queryWrapper.eq("device_name", deviceName);
        queryWrapper.orderByAsc("gmt_create"); // 按日期升序排序

        return bgDeviceHistoryLogService.list(queryWrapper);
    }
}

